//
//  JSMDreamCityViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/11/26.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMDreamCityViewController.h"
#import "JSMDreamCityModel.h"
#import "JSMDreamCityCollectionViewCell.h"
#import "JSMDreamCityPrefectureCollectionViewCell.h"
#import "JSMDreamCitySpecialDetailsViewController.h"
#import "JSMDreamCityPrefectureDetailsViewController.h"

@interface JSMDreamCityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, JSMDreamCityCollectionViewCellDelegate, JSMDreamCityPrefectureCollectionViewCellDelegate>

@property(nonatomic, strong) UISegmentedControl *segment;
@property(nonatomic, strong) BaseCollectionView *myCollection;

@end

@implementation JSMDreamCityViewController
#pragma mark - Method viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self createCollectionView];
    // 创建分段控件
    [self createSegment];
}
#pragma mark - createSegment
- (void)createSegment {
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"专题", @"专区"]];
    self.segment.frame = CGRectMake(0, 0, 200, 30);;
    self.segment.backgroundColor = [UIColor clearColor];
    [self.segment tintColorDidChange];
    self.segment.tintColor = [UIColor colorWithRed:0.995 green:0.990 blue:0.990 alpha:1.000];
    [self.segment setTintAdjustmentMode:UIViewTintAdjustmentModeAutomatic];
    self.segment.selectedSegmentIndex = 0;
    [self.segment addTarget:self action:@selector(segmentChangeAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.segment;
}
#pragma mark - Method segmentChangeAction:
- (void)segmentChangeAction:(UISegmentedControl *)segment {
    self.myCollection.contentOffset = CGPointMake(segment.selectedSegmentIndex * SCREENWIDTH, 0);
}
#pragma mark - Method scrollViewDidEndDecelerating:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.myCollection) {
        self.segment.selectedSegmentIndex = self.myCollection.contentOffset.x / SCREENWIDTH;
    }
}
#pragma mark - Method createCollectionView
- (void)createCollectionView {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT - 64);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.myCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 ) collectionViewLayout:flowLayout];
    self.myCollection.backgroundColor = [UIColor colorWithWhite:0.882 alpha:1.000];
    [self.view addSubview:self.myCollection];
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    self.myCollection.pagingEnabled = YES;
    self.myCollection.showsVerticalScrollIndicator = NO;
    self.myCollection.showsHorizontalScrollIndicator = NO;
    [self.myCollection registerClass:[JSMDreamCityCollectionViewCell class] forCellWithReuseIdentifier:@"myCollectionViewResue"];
    [self.myCollection registerClass:[JSMDreamCityPrefectureCollectionViewCell class] forCellWithReuseIdentifier:@"myPrefectureCollectionViewResue"];
}
#pragma mark - Method collectionView:numberOfItemsInSection:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
#pragma mark - Method collectionView:cellForItemAtIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        JSMDreamCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionViewResue" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    } else {
        JSMDreamCityPrefectureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myPrefectureCollectionViewResue" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
}
#pragma mark - Method 专题详情跳转
- (void)DreamCitySpecialPushToJSMDreamCitySpecialDetailsViewController:(JSMDreamCityModel *)jSMDreamCityModel {
    JSMDreamCitySpecialDetailsViewController *controller = [[JSMDreamCitySpecialDetailsViewController alloc] init];
    controller.tempModel = jSMDreamCityModel;
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method 专区详情跳转
- (void)DreamCityPrefecturePushToJSMDreamCityPrefectureDetailsViewController:(JSMDreamCityPrefectureModel *)dreamCityPrefectureModel {
    JSMDreamCityPrefectureDetailsViewController *controller = [[JSMDreamCityPrefectureDetailsViewController alloc] init];
    controller.tempModel = dreamCityPrefectureModel;
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
